// pages/appr/approval.js
let listBehavior = require('../../behavior/list')
Page({

  behaviors: [ listBehavior, ],
  /**
   * 页面的初始数据
   */
  data: {

    businessList:[],

    approveData:[
      {
        icon: 'icon-zhuijia',
        navText: '新申请',
        badge: 0
      },
      {
        icon: 'icon-gengduo',
        navText: '我审批',
        badge: 0,
        subNav: [{text: '待处理', badge: 0},
          {text: '已处理', badge: 0}],
        list: [],
        activeSubNav: 0,
        count: 0,
        post: {is_processed: false},
        url: 'Approval_process/query_approval_process_many_pending'
      },
      {
        icon: 'icon-shenpi1',
        navText: '已提交',
        badge: 0,
        list: [],
        count: 0,
        post: {},
        url: 'Approval_process/query_approval_process_many_send'
      },
      {
        icon: 'icon-account',
        navText: '抄送我',
        badge: 0,
        subNav: [{text: '已读', badge: 0},
          {text: '未读', badge: 0}],
        list: [],
        activeSubNav: 0,
        count: 0,
        url: 'Approval_process/query_approval_process_many_cc',
        post: {is_check:true}
      }
    ],
    activeNav: 0,

    //搜索条件
    keyList: [{field_id:'key_word',field_name: '申请人、摘要'}],
    searchList: [
      {field_id: 'sh_state',type: 'reference1',field_name: '审批状态',optionList: [
          {name: '待审',id: '待审'},
          {name: '复议',id: '复议'},
          {name: '已拒绝',id: '已拒绝'},
          {name: '已提交',id: '已提交'},
          {name: '已同意',id: '已同意'},
          {name: '已重新提交',id: '已重新提交'},
        ],optionName: 'name',optionId: 'id'},
      {field_id: 'table_id',type: 'reference1',field_name: '模板类型',
        optionList: [],optionName: 'menuName',optionId: 'table_id'},

      {field_id: 'input_id',type: 'reference1',field_name: '申请人',optionId:'id',optionName: 'name',},
      {field_id: ['date_e','date_s'],type: 'date',field_name: '提交日期'}
        ],
    searchObj: {}
  },

  //切换导航栏
  toggleNav(e){
    let {index} = e.currentTarget.dataset,
      {approveData} = this.data,
      obj = {activeNav: index,scrollTop: 0};

    this.setData(obj);

    if (approveData[index].url){

      this.getFirstPage()
    }

  },
  //切换审批过滤条件导航
  toggleFilter(e){
    let {index} = e.currentTarget.dataset,
      {activeNav,approveData} = this.data,
      data = approveData[activeNav],
      obj = {
      [`approveData[${activeNav}].activeSubNav`]: index
        ,scrollTop: 0
    }
    if (activeNav === 1){
      obj[`approveData[${activeNav}].post.is_processed`] = (index === 1)
    }
    if (activeNav === 3){
      obj[`approveData[${activeNav}].post.is_check`] = (index === 0)
    }
    this.setData(obj);
    this.getFirstPage()
  },

  navTo(e){
    let {url,tableid,rid} = e.currentTarget.dataset,
      {activeNav,approveData} = this.data;

    if (activeNav === 3 && approveData[activeNav].activeSubNav === 1){  //抄送未读-调用更新查看抄送数据接口
      this.updateApprovalCC(tableid,rid);
    }

    if(!url) {
      wx.$toast('小程序不支持该功能！')
      return
    }


    wx.navigateTo({
      url
    })
  },
  //搜索数据
  searchData({detail}){
    let searchObj = {};
    console.log(detail.comb)
    wx.$each(detail.comb,(i,item)=>{
      let val = item.CompareValue;
      if (item.CompareWith.indexOf('date')>=0){
        if (item.Operator === '>='){
          searchObj.date_s = val;
        }
        if (item.Operator === '<='){
          searchObj.date_e = val;
        }
      }else {
        searchObj[item.CompareWith] = val;
      }
    });
    this.setData({searchObj});
    let {activeNav, approveData} = this.data,
      data = approveData[activeNav];
    this.getFirstPage()
  },

  //获取新审批模块
  getApproveEntity(){
    wx.$post(3,'Menu/query_menu_qx_entity_approval').then(res=>{
      if (res.errcode != 0)return;
      let data = res.data.row || [];
      wx.$parseUrl(data)
      this.setData({businessList: data,['searchList[1].optionList']: data})
    })
  },
  //获取审批状态
  getProcessManyTotal(){
    wx.$post(3,'Approval_process/query_approval_process_many_total',{}).then(res=>{
      if (res.errcode != 0)return;
      let data = res.data.row[0];
      this.setData({
        [`approveData[1].badge`]: data.pending_totle,
        [`approveData[1].subNav[0].badge`]: data.pending_totle,
        [`approveData[3].badge`]: data.cc_totle,
        [`approveData[3].subNav[1].badge`]: data.cc_totle,
      })
    })
  },
  //获取审批记录
  getDataList(){
    let {activeNav, approveData, searchObj,page,limit} = this.data,
      data = approveData[activeNav],
      {url,post} = data,
      postData = {...post,...searchObj,page,limit};
    wx.$post(3,url,postData).then(res=>{
      if (res.errcode != 0)return;
      let list = res.data.row || [],
        total = res.data.total;
      if(page !== 1) list = [ ...data.list, ...list, ]
      let obj = {
          [`approveData[${activeNav}].list`]: list,
          total, page,
        };
      if (activeNav === 1 || activeNav === 3){  //我审批、抄送我导航栏显示数据数量
        obj[`approveData[${activeNav}].subNav[${data.activeSubNav}].badge`] = total;
      }

      this.setData(obj);
    })
  },
  //更新抄送
  updateApprovalCC(tableid,rowid){
    let
      url = 'Approval_process/update_approval_cc',
      post = {
        "table_id": tableid,
        "row_id": rowid,
      };
    wx.$post(3,url,post).then(()=>{
      this.getDataList();
    })

  },

  //获取审批人
  getUser(){
    wx.$get(3,'BaseTable/select_user').then(res=>{
      if (res.errcode != 0)return;
      let data = res.data.row || [];
      this.setData({['searchList[2].optionList']: data});
    })
  },

  ready(){},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.$setBarTitle();
    this.getApproveEntity();
    this.getUser();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getProcessManyTotal();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
